
function json2url(json) {
    let str=''
    for(let name in json){
        str+=name+'='+json[name]+'&'
    }
    return str.substring(0,str.length-1);
}

function http({url,data={},headers={},method='get'}) {
    if(!url)return;
    let baseURL='/api';
    headers['content-type']='application/json';
    //获取sessionStorage里面的token
    if(method.toLowerCase()=='get'){
        return fetch(baseURL+url+'?'+json2url(data),{headers}).then(res=>res.json());
    }else{
        return fetch(baseURL+url,{method,body:JSON.stringify(data),headers})
    }    
}
export default http;